package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.product.*;
import com.atguigu.gmall.product.service.ManagerService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Api(tags = "后台管理")
@RestController
@RequestMapping("/admin/product")
//@CrossOrigin
public class ManageController {

    @Autowired
    private ManagerService managerService;

    //获取一级分类
    @ApiOperation("获取一级分类")
    @GetMapping("/getCategory1")
    public Result getCategory1() {

        //获取一级分类
        List<BaseCategory1> baseCategory1List = managerService.getCategory1();
        return Result.ok(baseCategory1List);
    }

    //获取二级分类(根据一级分类id)
    @ApiOperation("获取二级分类")
    @GetMapping("/getCategory2/{category1Id}")
    public Result getCategory2(@PathVariable Long category1Id) {
        List<BaseCategory2> baseCategory2List = managerService.getCategory2(category1Id);
        return Result.ok(baseCategory2List);
    }

    //获取三级分类(根据二级分类id)
    @ApiOperation("获取三级分类")
    @GetMapping("getCategory3/{category2Id}")
    public Result getCategory3(@PathVariable Long category2Id) {
        List<BaseCategory3> baseCategory3List = managerService.getCategory3(category2Id);
        return Result.ok(baseCategory3List);
    }

    //根据分类id获取平台属性
    @ApiOperation("根据分类id获取平台属性")
    //http://api.gmall.com/admin/product/attrInfoList/{category1Id}/{category2Id}/{category3Id}
    @GetMapping("attrInfoList/{category1Id}/{category2Id}/{category3Id}")
    public Result attrInfoList(@PathVariable Long category1Id,
                               @PathVariable Long category2Id,
                               @PathVariable Long category3Id) {
        List<BaseAttrInfo> baseAttrInfoList = managerService.attrInfoList(category1Id, category2Id, category3Id);

        return Result.ok(baseAttrInfoList);
    }

    //保存平台属性
    //http://api.gmall.com/admin/product/saveAttrInfo
    @ApiOperation("保存平台属性及属性值")
    @PostMapping("/saveAttrInfo")
    public Result saveAttrInfo(@RequestBody BaseAttrInfo baseAttrInfo) {
        //保存2个表，平台属性，属性值
        managerService.saveAttrInfo(baseAttrInfo);
        return Result.ok();
    }

    //获取品牌分页列表 分页查询
    @ApiOperation("获取品牌分页列表 分页查询")
    @GetMapping("baseTrademark/{page}/{limit}")
    public Result baseTrademark(@PathVariable(name = "page") Integer page,
                                @PathVariable(name = "limit") Integer limit) {

        IPage<BaseTrademark> baseTrademarkIPage = managerService.baseTrademark(page, limit);

        return Result.ok(baseTrademarkIPage);
    }

    //获取spu分页列表,根据三级分类id
    @ApiOperation("获取spu分页列表,根据三级分类id")
    @GetMapping("/{page}/{limit}")
    public Result spuPage(
            @PathVariable(name = "page") Integer page,
            @PathVariable(name = "limit") Integer limit, Long category3Id) {
        //
        IPage<SpuInfo> spuInfoIPage = managerService.spuPage(page, limit, category3Id);

        return Result.ok(spuInfoIPage);
    }

    //查询所有品牌集合
    //baseTrademark/getTrademarkList
    @GetMapping("/baseTrademark/getTrademarkList")
    public Result getTrademarkList() {

        List<BaseTrademark> baseTrademarkList = managerService.getTrademarkList();
        return Result.ok(baseTrademarkList);
    }

    //获取销售属性
    @ApiOperation("获取销售属性")
    @GetMapping("/baseSaleAttrList")
    public Result baseSaleAttrList() {

        List<BaseSaleAttr> baseSaleAttrList = managerService.baseSaleAttrList();
        return Result.ok(baseSaleAttrList);
    }

    //保存商品信息 四张表
    @PostMapping("saveSpuInfo")
    public Result saveSpuInfo(@RequestBody SpuInfo spuInfo) {

        managerService.saveSpuInfo(spuInfo);

        return Result.ok();
    }

    //根据spuId获取图片列表
    @GetMapping("/spuImageList/{spuId}")
    public Result spuImageList(@PathVariable(name = "spuId") Long spuId) {
        //图片集合
        List<SpuImage> spuImageList = managerService.spuImageList(spuId);

        return Result.ok(spuImageList);
    }

    //根据spuId 查询销售属性及嵌套的属性值集合
    @GetMapping("/spuSaleAttrList/{spuId}")
    public Result spuSaleAttrList(@PathVariable(name = "spuId") Long spuId) {

        List<SpuSaleAttr> spuSaleAttrList = managerService.spuSaleAttrList(spuId);
        return Result.ok(spuSaleAttrList );

    }

    //保存Sku 四张表
    @PostMapping("/saveSkuInfo")
    public Result saveSkuInfo(@RequestBody SkuInfo skuInfo) {

        //保存
        managerService.saveSkuInfo(skuInfo);
        return Result.ok();
    }

    //查询Sku分页列表
    @GetMapping("/list/{page}/{limit}")
    public Result skuList(@PathVariable(name = "page") Integer page,
                          @PathVariable(name = "limit") Integer limit) {

        IPage<SkuInfo> skuInfoIPage = managerService.skuList(page, limit);

        return Result.ok(skuInfoIPage);
    }

    //上架
    @GetMapping("/onSale/{skuId}")
    public Result onSale(@PathVariable(name = "skuId") Long skuId) {
        managerService.onSale(skuId);
        return Result.ok();
    }

    //下架
    @GetMapping("/cancelSale/{skuId}")
    public Result cancelSale(@PathVariable(name = "skuId") Long skuId) {
        managerService.cancelSale(skuId);
        return Result.ok();
    }
}
